<?php

namespace app\admin\model\business;

use app\admin\model\basic\Assets;
use app\admin\model\report\Finance;
use think\Model;
use traits\model\SoftDelete;

class OrderProject extends Model
{

    use SoftDelete;

    // 表名
    protected $name = 'order_project';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';


    /**
     * Notes: 项目收入
     * User: AerYYY
     * Date: 2/6/24 9:46 PM
     * @param $params
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function income($params): bool
    {
        $project_id = $params['project_id'];
        $project_price = $params['project_price'];
        $project_remarks = $params['project_remarks'];

        $child = [];
        $price = 0;
        foreach ($project_id as $key => $item) {
            if (!empty($item) && !empty($project_price[$key]) && $project_price > 0) {
                $child[] = [
                    'order_no' => $params['order_no'],
                    'project_id' => $item,
                    'price' => $project_price[$key],
                    'remarks' => $project_remarks[$key] ?? '',
                ];
                $price = bcadd($price, $project_price[$key], 2);
            }
        }

        if (empty($price)) {
            $this->error = '错误的金额';
            return false;
        }

        if (count($child) <= 0) {
            $this->error = '子订单信息错误';
            return false;
        }

        $this->save([
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'unit_id' => $params['unit_id'],
            'staff_id' => $params['staff_id'],
            'assets_id' => $params['assets_id'],
            'price' => $price,
            'remarks' => $params['remarks'],
            'status' => Finance::STATUS_SR,
        ]);
        $orderProjectChildModel = new OrderProjectChild();
        $orderProjectChildModel->saveAll($child);

        $assetsModel = new  Assets();
        $assets = $assetsModel->where('id', $params['assets_id'])->find();
        if (empty($assets)) {
            $this->error = '错误的账户';
            return false;
        }
        $assets->balance = bcadd($assets->balance, $price, 2);
        $assets->save();

        $assetsLogModel = new AssetsLog();
        $data = [
            'staff_id' => $params['staff_id'],
            'assets_id' => $params['assets_id'],
            'status' => Finance::STATUS_SR,
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'be' => 1,
            'price' => $price,
        ];

        $assetsLogModel->createLog($data);

        $financeModel = new Finance();
        $params = [
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'staff_id' => $params['staff_id'],
            'unit_id' => $params['unit_id'],
            'be' => 1,
            'price' => $price,
            'status' => Finance::STATUS_SR
        ];
        $financeModel->save($params);

        return true;
    }

    /**
     * Notes: 项目支出
     * User: AerYYY
     * Date: 2/6/24 9:59 PM
     * @param $params
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function expenditure($params): bool
    {
        $project_id = $params['project_id'];
        $project_price = $params['project_price'];
        $project_remarks = $params['project_remarks'];

        $child = [];
        $price = 0;
        foreach ($project_id as $key => $item) {
            if (!empty($item) && !empty($project_price[$key]) && $project_price > 0) {
                $child[] = [
                    'order_no' => $params['order_no'],
                    'project_id' => $item,
                    'price' => $project_price[$key],
                    'remarks' => $project_remarks[$key] ?? '',
                ];
                $price = bcadd($price, $project_price[$key], 2);
            }
        }

        if (empty($price)) {
            $this->error = '错误的金额';
            return false;
        }

        if (count($child) <= 0) {
            $this->error = '子订单信息错误';
            return false;
        }

        $this->save([
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'unit_id' => $params['unit_id'],
            'staff_id' => $params['staff_id'],
            'assets_id' => $params['assets_id'],
            'price' => $price,
            'remarks' => $params['remarks'],
            'status' => Finance::STATUS_ZC,
        ]);
        $orderProjectChildModel = new OrderProjectChild();
        $orderProjectChildModel->saveAll($child);

        $assetsModel = new  Assets();
        $assets = $assetsModel->where('id', $params['assets_id'])->find();
        if (empty($assets)) {
            $this->error = '错误的账户';
            return false;
        }
        $assets->balance = bcsub($assets->balance, $price, 2);
        $assets->save();

        $assetsLogModel = new AssetsLog();
        $data = [
            'staff_id' => $params['staff_id'],
            'assets_id' => $params['assets_id'],
            'status' => Finance::STATUS_ZC,
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'be' => 2,
            'price' => $price,
        ];

        $assetsLogModel->createLog($data);

        $financeModel = new Finance();
        $params = [
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'staff_id' => $params['staff_id'],
            'unit_id' => $params['unit_id'],
            'be' => 2,
            'price' => $price,
            'status' => Finance::STATUS_ZC
        ];
        $financeModel->save($params);

        return true;
    }
}